#include "startwindow.h"
#include "rankdialog.h"
#include "ui_startwindow.h"
#include "config.h"
#include <QIcon>
#include <QPainter>
#include "helpdialog.h"

RankDialog* Rptr;

startwindow::startwindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::startwindow)
{
    ui->setupUi(this);
    initStartwindow();
}

startwindow::~startwindow()
{
    delete ui;
}

void startwindow::initStartwindow()
{
    //设置窗口的固定尺寸
    setFixedSize(Game_WIDTH,Game_HEIGHT);
    //设置标题
    setWindowTitle(Game_TITLE);
    //加载图标
    setWindowIcon(QIcon(Game_ICON));
    //load backgound
    m_background.load(GAME_START_BACKGOUND);

    Rptr= new RankDialog();
    Mptr= new MainWindow();

    //go to mainwidow
    connect(ui->Startbutton,SIGNAL(clicked(bool)),this,SLOT(on_Startbutton_clicked(bool)));

    //Go To Rankdialog
    connect(ui->Rankbutton,SIGNAL(clicked(bool)),this,SLOT(lick_GoTo_Rankdialog(bool)));

    //Rank界面发来信号显示Start界面
    connect(Rptr,SIGNAL(signalShowStartwindow()),this,SLOT(processShowStartwindow()));

}

void startwindow::on_Startbutton_clicked(bool)
{
    //发送信号，通知mainwidow窗体显示
    emit SignalShowMainwidow("ok");
    Mptr->show();
    Mptr->initSence();
    Mptr->playGame();
    this->hide();//发送信号后隐藏start界面
}

void startwindow::lick_GoTo_Rankdialog(bool)
{
    Rptr->readRank();
    Rptr->show();
    Rptr->setGeometry((this->width()-Rptr->width())/2,(this->height()-Rptr->height())/2,
                      Rptr->width(), Rptr->height());
}

void startwindow::processShowStartwindow()
{
    this->show();
}

void startwindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,m_background);
}

void startwindow::on_Rankbutton_2_clicked()
{
    sf=new HelpDialog();
    sf->show();

    sf->setGeometry((this->width()-Rptr->width())/2,(this->height()-Rptr->height())/2,
                      Rptr->width(), Rptr->height());
}
